package com.enlf.controller.admin;

import com.baomidou.mybatisplus.extension.conditions.query.QueryChainWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.enlf.dto.R;
import com.enlf.dto.UserDTO;
import com.enlf.entity.Func;
import com.enlf.entity.FuncRelation;
import com.enlf.service.IFuncRelationService;
import com.enlf.service.IFuncService;
import com.enlf.utils.EnlfConstants;
import com.enlf.utils.SystemConstants;
import com.enlf.utils.UserHolder;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.annotations.Param;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.Map;

/**
 * _*_ coding : utf-8 _*_
 *
 * @Time : 2024/3/18 22:20
 * @Author : 计201任思伟
 * @File : AdminFuncRelationController
 * @Project : enjoy-life
 */
@Slf4j
@RestController
@RequestMapping("/admin/func")
public class AdminFuncRelationController {

    @Resource
    private IFuncRelationService funcRelationService;
    @Resource
    private IFuncService funcService;

    /**
     * 为用户添加权限
     */
    @GetMapping("/add")
    public R addFuncRelation(@RequestParam(value = "userId") Long userId, @RequestParam(value = "funcId") Long funcId) {
        UserDTO userDTO = UserHolder.getUser();
        //查询用户是否有超级管理员权限
        FuncRelation isAdmin = funcRelationService.findByUserIdAdmin(userDTO.getId(), EnlfConstants.ADMIN_FUNC.FUNC_ADMIN.getFuncId());
        if (null == isAdmin){
            return R.FAIL("您无权限操作");
        }
        FuncRelation funcRelation = FuncRelation.builder()
                .funcId(funcId)
                .userId(userId)
                .processRelation("1")
                .addTime(LocalDateTime.now())
                .build();
        funcRelationService.save(funcRelation);
        return R.OK();
    }

    @GetMapping("/delete")
    public R deleteFuncRelation(@PathVariable("userId") Long userId, @PathVariable("funcId") Long funcId) {
        UserDTO userDTO = UserHolder.getUser();
        //查询用户是否有超级管理员权限
        FuncRelation isAdmin = funcRelationService.findByUserIdAdmin(userDTO.getId(), EnlfConstants.ADMIN_FUNC.FUNC_ADMIN.getFuncId());
        if (null == isAdmin){
            return R.FAIL("您无权限操作");
        }
        QueryChainWrapper<FuncRelation> funcRelationQueryChainWrapper = funcRelationService.query().eq("USER_ID", userId).eq("FUNC_ID",funcId);
        FuncRelation funcRelation = funcRelationQueryChainWrapper.one();
        funcRelation.setProcessRelation(EnlfConstants.ENLF_NUM_STR_0);
        funcRelationService.updateById(funcRelation);
        return R.OK();
    }

    /**
     * 权限列表
     */
    @PostMapping("/list")
    public R funcList(@RequestParam(value = "current", defaultValue = "1") Integer current) {
        UserDTO userDTO = UserHolder.getUser();
        //查询用户是否有超级管理员权限
        FuncRelation isAdmin = funcRelationService.findByUserIdAdmin(userDTO.getId(),EnlfConstants.ADMIN_FUNC.FUNC_ADMIN.getFuncId());
        if (null == isAdmin){
            return R.FAIL("您无权限操作");
        }
        // 根据类型分页查询
        Page<Func> page = funcService.query()
                .page(new Page<>(current, SystemConstants.DEFAULT_PAGE_SIZE_FIVE));
        // 返回数据
        Map<String,Object> result = new HashMap<>();
        result.put("total",page.getTotal());
        result.put("list",page.getRecords());
        return R.OK(result);
    }
}
